package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeBills;
import com.kehutong.charge.entity.ReceiptOrders;
import com.kehutong.charge.entity.ReceiptsStatistics;
import com.kehutong.charge.service.ReceiptOrdersService;
import com.kehutong.common.PageUtils;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: kht
 * @description:
 * @author: jian Ye
 * @create: 2021/8/3
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/bill/receipt/order")
public class ReceiptOrdersController {

    private static final Logger logger = LoggerFactory.getLogger(ReceiptOrdersController.class);

    @Inject
    ReceiptOrdersService receiptOrderService;



    @ReqMapping("/page")
    @HasPermission("crm:charge:receipt:order:view")
    public Object page(JSONObject jsonObject) {
        return receiptOrderService.findPage(jsonObject);
    }


    @ReqMapping("/page/export")
    @HasPermission("crm:charge:receipt:order:view")
    public void pageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        receiptOrderService.pageExport(req,resp,jsonObject);
    }


    @ReqMapping("/list")
    @HasPermission("crm:charge:receipt:order:view")
    public Object list(JSONObject jsonObject) {
        return receiptOrderService.findList(jsonObject);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("crm:charge:receipt:order:edit")
    @ReqMapping("/save")
    public Object save(ReceiptOrders entity, Token token) throws Exception {
        return receiptOrderService.save(entity,token);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("crm:charge:receipt:order:edit")
    @ReqMapping("/manualSave")
    public Object manualSave(ChargeBills chargeBill, Token token) throws Exception {
        return receiptOrderService.savePdf(chargeBill,token.getCompanyNo());
    }


    @Bean(copy=true)
    @HasPermission("crm:charge:receipt:order:edit")
    @ReqMapping("/update")
    public Object update(ReceiptOrders entity) throws Exception {

        return receiptOrderService.update(entity);
    }


    @HasPermission("crm:charge:receipt:order:edit")
    @ReqMapping("/delete")
    public Object delete(ReceiptOrders entity) {
        return receiptOrderService.deleteById(entity.getId());
    }


    @ReqMapping("/get")
    public Object get(ReceiptOrders entity) {
        return Result.success(entity);
    }


    /**
     * 统计功能
     */
    @ReqMapping("/receiptStat")
    @HasPermission("crm:charge:receipt:order:view")
    public Object receiptStat(JSONObject jsonObject,Token token) throws Exception {
        List<ReceiptsStatistics> listObj = receiptOrderService.groupData(jsonObject,token);
        return PageUtils.buildPage(jsonObject,listObj);
    }


}
